{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy\n",
    "from sklearn.preprocessing import normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict(row, coefficients):\n",
    "  yhat = coefficients[0]\n",
    "  for i in range(len(row)-1):\n",
    "    yhat += coefficients[i + 1] * row[i]\n",
    "  return yhat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def coefficients_sgd(train, l_rate, n_epoch):\n",
    "    coef = [0.0 for i in range(len(train[0]))]\n",
    "    print ('Coeficiente Inicial={0}' % (coef))\n",
    "    for epoch in range(n_epoch):\n",
    "        sum_error = 0\n",
    "        for row in train:\n",
    "            yhat = predict(row, coef)\n",
    "            error = yhat - row[-1]\n",
    "            sum_error += error**2\n",
    "            coef[0] = coef[0] - l_rate * error\n",
    "            for i in range(len(row)-1):\n",
    "                coef[i + 1] = coef[i + 1] - l_rate * error * row[i] \n",
    "        print(('epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error)))\n",
    "    return coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def load_csv(file):\n",
    "    x = []\n",
    "    y = []\n",
    "    for line in file:\n",
    "        x.append((line.split(';')[0:11]))\n",
    "        y.append((line.split(';')[11]))\n",
    "    return x, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = load_csv(open('winequality-white.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "del x[0]\n",
    "del y[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(len(x)):\n",
    "    for j in range(len(x[i])):\n",
    "        x[i][j] = float(x[i][j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(y)):\n",
    "    y[i] = float(y[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = x\n",
    "\n",
    "for i in range(len(dataset)):\n",
    "    dataset[i].append(y[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "normed_matrix = normalize(numpy.array(dataset), axis=1, norm='l1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Coeficiente Inicial={0}\n",
      "epoch=0, lrate=0.001, error=0.872\n",
      "epoch=1, lrate=0.001, error=0.576\n",
      "epoch=2, lrate=0.001, error=0.568\n",
      "epoch=3, lrate=0.001, error=0.561\n",
      "epoch=4, lrate=0.001, error=0.553\n",
      "epoch=5, lrate=0.001, error=0.546\n",
      "epoch=6, lrate=0.001, error=0.539\n",
      "epoch=7, lrate=0.001, error=0.532\n",
      "epoch=8, lrate=0.001, error=0.526\n",
      "epoch=9, lrate=0.001, error=0.519\n",
      "[0.029268124428863718, 0.006060325343037163, 0.00023158361651062572, 0.00027038893949628371, -0.00064426131038343155, 2.4512416662550594e-05, -0.0013512180550787187, 0.0028087110725686739, 0.00089671800063447025, 0.0029001252239525175, 0.00040917191688755942, 0.011352776688612832]\n"
     ]
    }
   ],
   "source": [
    "l_rate = 0.001\n",
    "n_epoch = 10\n",
    "coef = coefficients_sgd(normed_matrix, l_rate, n_epoch)\n",
    "print(coef)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
